﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using Kaidou.Util;
using Kaidou.Bll;
using System.Collections.Generic;
using System.Globalization;

public partial class seal_seal_ordered_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         LoginModel loginModel = (LoginModel)Session["loginModel"];
        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }

        if (loginModel.isKumiaiin())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        if (!IsPostBack)
        {

            try
            {

                Order order = new Order();
                TRN_Juchu_HPersister parH = new TRN_Juchu_HPersister();
                TRN_Juchu_DPersister parD = new TRN_Juchu_DPersister();
                string juchu_no = Request.QueryString["juchu_no"].ToString();
                TrnJuchuh h = parH.getDetail(juchu_no);
                TrnJuchud[] d = parD.getDetail(juchu_no);
                order.setJuchu_H(h);
                order.setJuchu_D(d);
                Session["order"] = order;

                MST_UnionPersister dao1 = new MST_UnionPersister();
                MST_Union_BranchPersister dao2 = new MST_Union_BranchPersister();
                MstUnion union = dao1.getDetail(h.UnionCode);
                mst_union_branch branch = dao2.getDetail(h.UnionCode, h.BranchCode);
                string hachu_union_name = "";
                if (h.HachuUnionCode != null)
                {
                    if (h.HachuBranchCode > 0)
                    {
                        mst_union_branch m1= dao2.getDetail(h.HachuUnionCode, h.HachuBranchCode);
                        hachu_union_name = m1!=null?m1.officename:"";
                    }
                    else
                    {
                        MstUnion m2 = dao1.getDetail(h.HachuUnionCode);
                        hachu_union_name = m2!=null?m2.OfficeName:"";
                    }
                }


                if (TrnJuchuh.JUCHU_CLSS_INTERNET.Equals(h.JuchuClss))
                {
                    htitle.Text = "インターネット";
                }
                else if (TrnJuchuh.JUCHU_CLSS_FAX.Equals(h.JuchuClss))
                {
                    htitle.Text = "FAX/OCR";
                }

                JUCHU_DATE.Text = h.JuchuDate == null ? "" : ((DateTime)h.JuchuDate).ToString("yyyy年MM月dd日");
                if (h.FaxRcvDate != null && !h.FaxRcvDate.ToString().Equals(""))
                {
                    sx.Visible = false;
                    faxrcvdate.Text = ((DateTime)h.FaxRcvDate).ToString("yyyy年MM月dd日 hh:mm");
                }
                MST_UNION_CODE.Text = h.UnionCode;
                ParentOfficeName.Text = union == null ? "" : union.OfficeName;
                MST_BRANCH_CODE.Text = h.BranchCode == 0 ? "000" : String.Format("{0:D3} ", h.BranchCode);
                Branch_office_name.Text = branch == null ? "本店" : branch.officename;
                TRN_JUCHU_TANTO_NAME.Text = h.JuchuTantoName;

                if (h.HopeDate != null)
                {
                    DateTime dt = (DateTime)h.HopeDate;
                    TRN_HOPE_DATE.Text = dt.ToString("yyyy年MM月dd日");

                }
                else
                {
                    TRN_HOPE_DATE.Text = "指定なし";
                }

                if (TrnJuchuh.HOPE_TIME_CLSS_NONE.Equals(h.HopeTimeClss))
                {
                    TRN_HOPE_TIME_CLSS.Text = "指定なし";
                }

                if (TrnJuchuh.HOPE_TIME_CLSS_MORNING.Equals(h.HopeTimeClss))
                {
                    TRN_HOPE_TIME_CLSS.Text = "午前";
                }

                if (TrnJuchuh.HOPE_TIME_CLSS_AFTERNOON.Equals(h.HopeTimeClss))
                {
                    TRN_HOPE_TIME_CLSS.Text = "午後";
                }

                string area = Resources.Resource.area;
                string[] areaArray = area.Split(',');

                if (branch != null)
                {

                    addressmsg.Text = "〒" + branch.officezipno.Substring(0, 3) + "-" + branch.officezipno.Substring(3, 4) + "<br/>" + areaArray[branch.officeprefcode-1] + " " + branch.officecity + " " + branch.officeaddress + "<br/>" +
                        "TEL：" + branch.officetel + " FAX：" + branch.officefax;

                }
                else if (h.BranchCode == 0 && union != null)
                {
                    addressmsg.Text = "〒" + union.OfficeZipNo.Substring(0, 3) + "-" + union.OfficeZipNo.Substring(3, 4) + "<br/>" + areaArray[union.OfficePrefCode-1] + " " + union.OfficeCity + " " + union.OfficeAddress + "<br/>" +
                        "TEL：" + union.OfficeTel + " FAX：" + union.OfficeFax;
                }


                TRN_SEAL_QTY1.Text = d[0] == null ? "" : (d[0].SealQty == 0 ?"": d[0].SealQty.ToString());
                seal_str_end_no_1.Text = d[0] == null ? "～" : (d[0].SealQty == 0 ?"" :(d[0].SealStrNo + "～" + d[0].SealEndNo));

                Kaidou.Persister.MST_TraderPersister traderDao = new Kaidou.Persister.MST_TraderPersister();
                MstTrader trader = null;

                if (d[1] != null)
                {
                    trader = traderDao.getDetail(d[1].TraderRegNo);
                    TRADER_REG_NO2.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL2.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY2.Text = d[1] != null ? d[1].SealQty.ToString() : "";
                    seal_str_end_no_2.Text = d[1] == null ? "～" : d[1].SealStrNo + "～" + d[1].SealEndNo;
                }
                else
                {
                    seal_str_end_no_2.Text = "～";
                }

                if (d[2] != null)
                {
                    trader = traderDao.getDetail(d[2].TraderRegNo);
                    TRADER_REG_NO3.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL3.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY3.Text = d[2] != null ? d[2].SealQty.ToString() : "";
                    seal_str_end_no_3.Text = d[2] == null ? "～" : d[2].SealStrNo + "～" + d[2].SealEndNo;
                }
                else
                {
                    seal_str_end_no_3.Text = "～";
                }

                if (d[3] != null)
                {
                    trader = traderDao.getDetail(d[3].TraderRegNo);
                    TRADER_REG_NO4.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL4.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY4.Text = d[3] != null ? d[3].SealQty.ToString() : "";
                    seal_str_end_no_4.Text = d[3] == null ? "～" : d[3].SealStrNo + "～" + d[3].SealEndNo;
                }
                else
                {
                    seal_str_end_no_4.Text = "～";
                }

                if (d[4] != null)
                {
                    trader = traderDao.getDetail(d[4].TraderRegNo);
                    TRADER_REG_NO5.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL5.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY5.Text = d[4] != null ? d[4].SealQty.ToString() : "";
                    seal_str_end_no_5.Text = d[4] == null ? "～" : d[4].SealStrNo + "～" + d[4].SealEndNo;
                }
                else
                {
                    seal_str_end_no_5.Text = "～";
                }

                sum.Text = h.TotalSealQty.ToString();

               // ocr_order_qty.Text = h.OcrOrderQty.ToString();

                hachu_date.Text = h.HachuDate == null ? "" : ((DateTime)h.HachuDate).ToString("yyyy年MM月dd日");
                hachu_union_office_name.Text = hachu_union_name;


                if (h.DelivDate != null)
                {
                    DELIV_YY.Text = ((DateTime)h.DelivDate).ToString("yy");
                    DELIV_MM.Text = ((DateTime)h.DelivDate).ToString("MM");
                    DELIV_DD.Text = ((DateTime)h.DelivDate).ToString("dd");
                }
                
                DELIV_TANTO_NAME.Text = h.DelivTantoName == null ? "" : h.DelivTantoName;
                DELIV_COMP_NAME.Text = h.DelivCompName == null ? "" : h.DelivCompName;
                DELIV_NO.Text = h.DelivNo == null ? "" : h.DelivNo;



            }
            catch (Exception ex)
            {

                LogManager.error("seal_seal_ordered_detail " + ex.Message + ex.StackTrace);
            }
        }
    }

    protected void goback(object sender, EventArgs e)
    {
        Session["back"] = 1;
        Response.Redirect("seal_order_search.aspx");

    }

    protected void update(object sender, EventArgs e)
    {
        Order order = (Order)Session["order"];

        try
        {
           TrnJuchuh h = order.getJuchu_H();

           h.DelivYear = DELIV_YY.Text;
           h.DelivMonth = DELIV_MM.Text;
           h.DelivDay = DELIV_DD.Text;

           if (StringUtil.isValidDate(DELIV_YY.Text, DELIV_MM.Text, DELIV_DD.Text))
           {
               CultureInfo culture = new CultureInfo("ja-JP");
               h.DelivDate = Convert.ToDateTime((int.Parse(DELIV_YY.Text) + 2000) + "/" + int.Parse(DELIV_MM.Text) + "/" + int.Parse(DELIV_DD.Text, culture));

           }
             
            h.DelivTantoName = DELIV_TANTO_NAME.Text;
            h.DelivCompName = DELIV_COMP_NAME.Text;
            
            h.DelivNo = DELIV_NO.Text.Trim().Length == 0 ? null : DELIV_NO.Text;
          
            order.setJuchu_H(h);

            //検証
            LoginModel loginModel = (LoginModel)Session["loginModel"];
            TRN_JuchuManager validation = new TRN_JuchuManager();
            List<string> errorCodeList = validation.checkUpdateOrdered(order, Resources.Resource.ResourceManager);
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count == 0)
            {
                Session["order"] = order;
                Response.Redirect("seal_ordered_confirm.aspx");
            }

        }
        catch (Exception ee)
        {
            LogManager.error("seal_seal_ordered_detail error :"+ee.Message+ee.StackTrace);
        }
		
    }

    
    protected void viewChumon(object sender, EventArgs e)
    {
        Order order = ( Order ) Session["order"];
        TrnJuchuh h = order.getJuchu_H();
        //Response.Redirect("seal_order_paper.aspx?juchu_no="+h.JuchuNo);
        string url = "seal_order_paper.aspx?juchu_no=" + h.JuchuNo;
        Response.Write("<script language=\"javascript\">window.open('" + url + "','_blank');</script>");
    }
    

}
